<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AutoEvaluacionAuthorizationPlugin
 *
 * @export Plugins
 */
class AutoEvaluacionAuthorizationPlugin extends UnderscorePlugin implements BeforeTask {

    /**
     *
     * @var Request
     */
    private $request;
    private $idsRealizadas = array();
    private $formId;
    private static $Users = array(
        "7" => array(
            "fhernandez",
            "jagudelo",
            "jrhenals",
            "kposada",
            "lvilladiego",
            "lbenitez",
            "apizarro",
            "avillafane",
            "dbarrios",
            "fvergara",
            "ibedoya",
            "kfernandez",
            "mmoraleso",
            "mcabrales",
            "nsanchez",
            "nnino",
            "rromero",
            "alozano",
            "vmarrugo",
            "rtaylor",
            "norozco",
            "mlora",
            "nloiza",
            "ccortez",
            "eviloria",
            "amontoya",
            "ggonzalez",
            "jjimenezb",
            "ovillareal",
            "adiaz2",
            "lhoyos",
            "havilan",
            "dberrocal",
            "ybarguil",
            "npaniza",
            "emagri",
            "ckafury",
            "rruiz",
            "vgomez",
            "grodriguez",
            "lmontes",
            "abatista",
            "mestor",
            "jbarrios",
            "abozzi",
            "cmeza",
            "dbuendia",
            "erodriguezc",
            "eribon",
            "Josorio",
            "jtorres",
            "lsanchez",
            "nochoa",
            "smartinez",
            "tzuniga",
            "amartelo",
            "kibarra",
            "ilicero",
            "jcarrascal",
            "jpena",
            "anavarro",
            "fpena",
            "amunoz",
            "evaleta",
            "rropain",
            "mjaramillo",
        ),
        "10" => array(
            "fhernandez",
            "jagudelo",
            "jrhenals",
            "kposada",
            "lvilladiego",
            "lbenitez",
            "apizarro",
            "avillafane",
            "dbarrios",
            "fvergara",
            "ibedoya",
            "kfernandez",
            "mmoraleso",
            "mcabrales",
            "nsanchez",
            "nnino",
            "rromero",
            "alozano",
            "vmarrugo",
            "rtaylor",
            "norozco",
            "mlora",
            "nloiza",
            "ccortez",
            "eviloria",
            "amontoya",
            "ggonzalez",
            "jjimenezb",
            "ovillareal",
            "adiaz2",
            "lhoyos",
            "havilan",
            "dberrocal",
            "ybarguil",
            "npaniza",
            "emagri",
            "ckafury",
            "rruiz",
            "vgomez",
            "grodriguez",
            "lmontes",
            "abatista",
            "mestor",
            "jbarrios",
            "abozzi",
            "cmeza",
            "dbuendia",
            "erodriguezc",
            "eribon",
            "Josorio",
            "jtorres",
            "lsanchez",
            "nochoa",
            "smartinez",
            "tzuniga",
            "amartelo",
            "kibarra",
            "ilicero",
            "jcarrascal",
            "jpena",
            "anavarro",
            "fpena",
            "amunoz",
            "evaleta",
            "rropain",
            "mjaramillo",
        ),
    );

    public function beforeTask($task) {
        $this->request = ModulesContext::instance()->getRequest();
        $this->formId = $this->request->get("formId");

        if ($this->getModule()->getName() == "InscripcionesGenericas" && array_key_exists($this->formId, self::$Users)) {

            $encuestasRealizadas = file_get_contents(Loader::toSinglePath("ext::plugins::encuestasRealizadas_{$this->formId}", ".txt"));
            $this->idsRealizadas = explode(",", $encuestasRealizadas);

            $session = $this->request->getSession();

            if (!$session->state() === SessionStates::ACTIVE || !$this->checkRoles()) {
                $returnUrl = $this->request->getURI();
                $router = ModulesContext::instance()->getRouting();
                //This is a joomla specific action.
                $url = $this->getOnPermissionDeniedURL() . "&return=" . base64_encode($returnUrl);
                $router->redirect($url);
            }

            if ($task == "guardar") {
                $this->onGuardarencuesta();
            }
        }

        return true;
    }

    private function getOnPermissionDeniedURL() {
        return ModulesContext::instance()->getConfig()->get("PermissionDeniedURL");
    }

    private function checkRoles() {

        $result = false;
        $user = ModulesContext::instance()->getCurrentUser();
        $userName = $user->getData()->get("alterUserName");

        if (in_array($userName, self::$Users[$this->formId]) && !in_array($userName, $this->idsRealizadas)) {
            $result = true;
        }

        return $result;
    }

    private function onGuardarencuesta() {
        $user = ModulesContext::instance()->getCurrentUser();
        $userName = $user->getData()->get("alterUserName");
        $this->idsRealizadas[] = $userName;
        $encuestasRalizadas = implode(",", $this->idsRealizadas);
        file_put_contents(Loader::toSinglePath("ext::plugins::encuestasRealizadas_{$this->formId}", ".txt"), $encuestasRalizadas);
    }

}

?>
